#!/bin/python3
#-*- coding: UTF-8 -*-

'''
       _ _             _____                 
      | (_)           |  __ \                
      | |_ _ __   __ _| |__) |__ _ __   __ _ 
  _   | | | '_ \ / _` |  ___/ _ \ '_ \ / _` |
 | |__| | | | | | (_| | |  |  __/ | | | (_| |
  \____/|_|_| |_|\__, |_|   \___|_| |_|\__, |
                  __/ |                 __/ |
                 |___/                 |___/ 
'''

from fileinput import filename
import os
import abc
from PIL import Image
from enum import Enum

class Image_e(Enum):
    JPG  = 0
    BMP  = 1
    JPEG = 2
    PNG  = 4
    MAX  = PNG + 1

# 抽象类
class asbImager(metaclass=abc.ABCMeta):
    all_type='file'
    @abc.abstractmethod # 定义抽象方法
    def image_convert(self):
        pass


# 图片转换父类实现复用接口类
class Image_convert(asbImager):
    current_dir   = ""
    target_dir    = ""
    current_type  = ""
    target_type   = ""
    convert_count = 0
    dir_list      = []
    def __init__(self) -> None:
        super().__init__()
    def __init__(self, cur_type, tar_type):
        self.current_type  = cur_type   # 确定当前类型
        self.target_type   = tar_type   # 确定目标类型
        self.get_current_dir()          # 获取当前目录
        self.get_dir_list()             # 获取当前文件列表

    def __del__(self):
        pass

    def get_current_dir(self):
        self.current_dir = os.getcwd()
    
    def get_dir_list(self):
        if self.current_dir != "":
            self.dir_list = os.listdir(self.current_dir)

    def create_target_dir(self, dir_name):
        if self.current_dir != "":
            try :
                os.mkdir(self.current_dir + "/" + dir_name)
            except Exception as re:
                print("%s 已经存在", dir_name)

    def image_convert(self):
        pass


# 实现JPG转BMP
class jpg2bmp(Image_convert):
    def convert(self, target_type):
        self.convert_count = 0
        self.target_type  = "." + self.target_type.lower()
        self.target_dir   = self.current_dir + "/" + target_type +"/"
        self.get_dir_list()
        
        for fileName in self.dir_list:
            if os.path.splitext(fileName)[1] == '.jpg' or os.path.splitext(fileName)[1] == '.JPG' :
                name = os.path.splitext(fileName)[0]
                newFileName = name + self.target_type

                img = Image.open(self.current_dir + "/" + fileName)
                img.save(self.target_dir + newFileName)
                self.convert_count += 1
                print(f" {fileName} \t\t\t转换为: {newFileName}")
        print("%s 转为 %s 文件总个数为: %d",self.current_type, self.target_type, self.convert_count)

    def image_convert(self):
        cur_dst_dir =  os.getcwd()
        self.current_dir = os.getcwd()
        
        self.create_target_dir("BMP")
        self.convert("BMP")


# 实现BMP转JPG
class bmp2jpg(Image_convert):
    def convert(self, target_type):
        self.convert_count = 0
        self.target_type  = "." + self.target_type.lower()
        self.target_dir   = self.current_dir + "/" + target_type +"/"
        self.get_dir_list()
        
        for fileName in self.dir_list:
            if os.path.splitext(fileName)[1] == '.bmp' or os.path.splitext(fileName)[1] == '.BMP' :
                name = os.path.splitext(fileName)[0]
                newFileName = name + self.target_type

                img = Image.open(self.current_dir + "/" + fileName)
                img.save(self.target_dir + newFileName)
                self.convert_count += 1
                print(f" {fileName} \t\t\t转换为: {newFileName}")
        print("%s 转为 %s 文件总个数为: %d",self.current_type, self.target_type,self.convert_count)


    def image_convert(self):
        cur_dst_dir =  os.getcwd()
        self.current_dir = os.getcwd()
        
        self.create_target_dir("JPG")
        self.convert("JPG")


# 调用层
def ImageConvert(cur_type, tar_type):
    match cur_type:
        case "JPG":
            jpg2bmp_t = jpg2bmp(cur_type, tar_type)
            jpg2bmp_t.image_convert()
        case "BMP":
            
            bmp2jpg_t = bmp2jpg(cur_type, tar_type)
            bmp2jpg_t.image_convert()
        case _: print("类型输入错误")
    pass


# 入口
def main():
    ImageConvert( "JPG", "BMP")
    input("转换完成, 请按Enter键退出...")
    return 0


if __name__ == '__main__':
    main()
